package com.cy.pj.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.cy.pj.sys.pojo.Category;
import com.cy.pj.sys.service.SysLogService;

/**
 * Eexel导出
 * @author Administrator
 *
 */
@RestController
public class ExprotHandlerController {

	@Autowired
	private SysLogService sysLogService;
	
    //@GetMapping("/exportExcel")
    @RequestMapping("/exportExcel")
    public void export(HttpServletResponse response) throws IOException {
    	List<Category> list = sysLogService.findExcel();
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        String fileName = "日志信息表";
        Sheet sheet = new Sheet(1, 0,Category.class);
        //设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        // 第一个 sheet 名称
        sheet.setSheetName("日志信息");
        writer.write(list, sheet);
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
        writer.finish();
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        out.flush();
        out.close();
    }
}
